package com.quotes.dates;

import java.util.Calendar;
import java.util.Date;

public class TimeRangeFactory 
{
	public static TimeRange untilToday(TimePeriod lenghtOfPeriod) {
		Date end = getToday();
		Date start = substractByPeriod(getToday(), lenghtOfPeriod);
		return new TimeRange(start, end);
	}
	
	public static Date substractByPeriod(Date date, TimePeriod distance) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
			
		switch (distance) {
			case ONE_MONTH: calendar.add(Calendar.MONTH, -1); break;
			case THREE_MONTHS: calendar.add(Calendar.MONTH, -3); break;
			case SIX_MONTHS: calendar.add(Calendar.MONTH, -6); break;
			case ONE_YEAR: calendar.add(Calendar.YEAR, -1); break;
			case TWO_YEARS: calendar.add(Calendar.YEAR, -2); break;
			case THREE_YEARS : calendar.add(Calendar.YEAR, -3); break;
		}
		
		return calendar.getTime();
	}
	
	public static Date getToday() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
}
